<?php

namespace app\admin\controller;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Db;
use think\Session;

class Order extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function user(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['yy_type'] = 1;
            if(isset($number)&&$number!="")
            {
                $map['yy_number'] = ['like',"%" . $number . "%"];
            }
            if(isset($phone)&&$phone!="")
            {
                $map['user_phone'] = ['like',"%" . $phone . "%"];
            }
            if(isset($username)&&$username!="")
            {
                $map['user_username|user_shopname'] = ['like',"%" . $username . "%"];
            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_yuyue')->alias('y')
                ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
                ->where($map)->count();
            $field = 'yy_number,yy_status,yy_weight,yy_createtime,yy_del';
            $field .= ',user_id,user_shopname,user_username,user_image,user_phone,user_gender';
            $lists = Db::table('up_yuyue')->alias('y')
                ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
                ->where($map)
                ->field($field)
                ->page($Nowpage,$limits)
                ->order('yy_status ASC,yy_createtime desc')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }

        return $this->fetch("order/user");
    }
    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function daili(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['yy_type'] = 2;
            if(isset($number)&&$number!="")
            {
                $map['yy_number'] = ['like',"%" . $number . "%"];
            }
            if(isset($phone)&&$phone!="")
            {
                $map['user_phone'] = ['like',"%" . $phone . "%"];
            }
            if(isset($username)&&$username!="")
            {
                $map['user_username|user_shopname'] = ['like',"%" . $username . "%"];
            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_yuyue')->alias('y')
                ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
                ->where($map)->count();
            $field = 'y.*';
            $field .= ',user_id,user_shopname,user_username,user_image,user_phone,user_gender';
            $lists = Db::table('up_yuyue')->alias('y')
                ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
                ->where($map)
                ->field($field)
                ->page($Nowpage,$limits)
                ->order('yy_status ASC,yy_createtime desc')
                ->select();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }

        return $this->fetch("order/daili");
    }


    /**
     * [userAdd 更改预约状态：只是代理的：已确认和拒绝（已取消）]
     * @return [type] [description]
     * @author
     */
    public function agreeyy()
    {
        extract(input());
        $res = Db::table('up_yuyue')
            ->where('yy_id',$id)
            ->update(['yy_status'=>$status]);
        if($res){
            return json(['code' => 200, 'data' => '', 'msg' => '操作成功']);
        }else{
            return json(['code' => 0, 'data' => '', 'msg' => '操作失败']);
        }

    }
    /**
     * [userAdd 更改预约状态：已完成：打钱]
     * @return [type] [description]
     * @author
     */
    public function moneyyy()
    {
        extract(input());
        $yy = Db::table('up_yuyue')
            ->where('yy_id', $id)
            ->field('yy_user_id,yy_admin_status,yy_status,yy_admin_money,yy_admin_image,yy_number,yy_createtime')
            ->find();
        if ($yy['yy_admin_status'] == 2 || $yy['yy_status'] == 3 || $yy['yy_admin_money']) {
            return json(['code' => 0, 'data' => '', 'msg' => '已经打过钱了']);
        }
        $money = sprintf("%.2f",$money);
        if ($money < 0) {
            return json(['code' => 0, 'data' => '', 'msg' => '金额不能小于0']);
        }
        if ($yy['yy_admin_status'] == 1 && $yy['yy_status'] == 2 && $yy['yy_admin_image']) {
            Db::startTrans();
            try {
                $update = array();
                $update['yy_admin_money'] = $money;
                $update['yy_admin_status'] = 2;
                $update['yy_status'] = 3;
                //1、改状态
                Db::table('up_yuyue')->where('yy_id', $id)->update($update);
                //2、代理加钱
                Db::table('up_u_user')->where('user_id', $yy['yy_user_id'])->setInc('user_money',$money);
                //3、入通知且入记录
                allInNotice('预约单金额到账',"您于 {$yy['yy_createtime']} 提交的单号为 {$yy['yy_number']} 的预约单已经完成，金额为 $money",$yy['yy_user_id'],$id);
                allInJilu($yy['yy_user_id'],$yy['yy_number'],'预约单结算',1,3,$money);
                Db::commit();
                return json(['code' => 200, 'data' => '', 'msg' => '操作成功']);
            } catch (\Exception $e) {
                Db::rollback();
                return json(['code' => 0, 'data' => '', 'msg' => '操作失败']);
            }
        } else {
            return json(['code' => 0, 'data' => '', 'msg' => '请刷新重试']);
        }


    }


    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function orderInfo(){
        extract(input());
        $field = 'y.*';
        $field .= ',user_id,user_shopname,user_username,user_image,user_phone,user_gender';
        $data = Db::table('up_yuyue')->alias('y')
            ->join('up_u_user u','y.yy_user_id=u.user_id','LEFT')
            ->where('yy_id',$id)
            ->field($field)
            ->find();
        if($data['yy_admin_image']){
            $data['yy_admin_image'] = explode(',',$data['yy_admin_image']);
        }
        $this->assign('data',$data);
        return $this->fetch("order/user_order_info");
    }
}